<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class orderModel extends Model
{
    use SoftDeletes;
    protected $table = 'order';
    protected $primaryKey = 'id';
    protected $appends = ['paytime_text'];

    protected $hidden = ['paytime'];
    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected static function booted()
    {
        //限定查询自营全局作用域
        static::addGlobalScope('dianpu_id', function (\Illuminate\Database\Eloquent\Builder $builder) {
            $builder->where('dianpu_id', '=', 0);
        });
    }

    public function getPaytimeTextAttribute()
    {
        $paytime_text = '';

        if($this->attributes['paytime']){
            $paytime_text = date('Y-m-d H:i:s', $this->attributes['paytime']);
        }else{
            $paytime_text = '无支付时间';
        }

        return $paytime_text;
    }

    public function orderItems()
    {
        return $this->hasMany(orderItemModel::class, 'order_id', 'id');
    }

    public function dianpu()
    {
        return $this->belongsTo(dianpuModel::class, 'dianpu_id', 'id')->select(['id', 'name', 'company_name', 'phone']);
    }
}